﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NIEN_LUAN
{
    public partial class FormCongNoKhachHang : Form
    {
        private Db.DataContentDataContext db = new NIEN_LUAN.Db.DataContentDataContext();

        public FormCongNoKhachHang()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }       

        private void FormCongNoKhachHang_Load(object sender, EventArgs e)
        {
            this.dataGridViewcongno.AutoGenerateColumns = false;
            this.dataGridViewcongno.DataSource = this.bsNo;

            var khachhang = (from m in db.KHACHHANGs
                             select m).ToList();
            this.bskhachhang.DataSource = khachhang;
            this.comboBoxKH.DataSource = bskhachhang;
            this.comboBoxKH.DisplayMember = "MaKhachHang";
        }     

        private void btnChonKH_Click(object sender, EventArgs e)
        {
            Form frmChonKH = new FormTimKiemKhachHang(this);
            frmChonKH.ShowDialog();
        }

        private void btnCongno_Click(object sender, EventArgs e)
        {
            //if (this.radioButtonKH.Checked == false && this.radioButtonTien.Checked == false)
            //{              

            //    var no = (from m in db.CHUNGTUs
            //              // where ((m.MaKhachHang == this.comboBoxKH.Text) &&  ((m.TongTien.Value-m.TienDaTra.Value) <= this.numericUDTienNo.Value))
            //              select new
            //              {
            //                  MaKhachHang = m.KHACHHANG.MaKhachHang,
            //                  TenKhachHang = m.KHACHHANG.TenKhachHang,
            //                  SoTienNo = m.TongTien - (from k in db.PHIEUTHUs
            //                                           where k.SoChungTu == m.SoChungTu
            //                                           select k).Sum(s => s.SoTien) + m.TienDaTra
            //              }).Distinct().ToList();
            //    this.bsNo.DataSource = no;
            //    this.bsNo.ResetBindings(true);
            //}

            //if (this.radioButtonKH.Checked == true && this.radioButtonTien.Checked ==false )
            //{
                if (string.IsNullOrEmpty(this.comboBoxKH.Text))
                {
                    MessageBox.Show("Chưa nhập tên khách hàng", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                var no = (from m in db.CHUNGTUs
                          where (m.MaKhachHang == this.comboBoxKH.Text)
                          select new
                          {
                              MaKhachHang = m.KHACHHANG.MaKhachHang,
                              TenKhachHang = m.KHACHHANG.TenKhachHang,
                              SoTienNo = m.TongTien - ((from k in db.PHIEUTHUs
                                                       where k.SoChungTu == m.SoChungTu
                                                       select k).Sum(s=>s.SoTien))//-m.TienDaTra//.Sum(s => s.SoTien) + m.TienDaTra
                          }).ToList();
                this.bsNo.DataSource = no;
                this.bsNo.ResetBindings(true);
            var no2 = (from m in no
                            from x in no
                          where m.MaKhachHang==x.MaKhachHang
                          select new {
                              MaKhachHang = m.MaKhachHang,
                              TenKhachHang = m.TenKhachHang,
                              SoTienNo =no.Sum(k=>k.SoTienNo)////????


                          }).Distinct().ToList();
          
                this.bsNo.DataSource = no2;
                this.bsNo.ResetBindings(true);



             //var no = (from m in db.PHIEUNHAPs
             //             where (m.MaNhaCungCap == this.comboBoxNCC.Text)
             //             select new
             //             {
             //                 MaNhaCungCap = m.NHACUNGCAP.MaNhaCungCap,
             //                 TenNhaCungCap = m.NHACUNGCAP.TenNhaCungCap,
             //                 SoTienNo = m.TongTien - m.TienDaTra
             //             }).ToList();
             //   var no2 = (from m in no
             //               from x in no
             //             where m.MaNhaCungCap==x.MaNhaCungCap
             //             select new {
             //                 MaNhaCungCap = m.MaNhaCungCap,
             //                 TenNhaCungCap = m.TenNhaCungCap,
             //                 SoTienNo =no.Sum(k=>k.SoTienNo)////????


             //             }).Distinct().ToList();
             //   this.bsNo.DataSource = no2;
             //   this.bsNo.ResetBindings(true);
            //}

            //if (this.radioButtonKH.Checked == false && this.radioButtonTien.Checked == true)
            //{
            //    if (this.numericUDTienNo.Value <= 0)
            //    {
            //        MessageBox.Show("Chưa chọn số tiền nợ", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //        return;
            //    }

            //    var no = (from m in db.CHUNGTUs
            //              where ((m.TongTien.Value-m.TienDaTra.Value) <= this.numericUDTienNo.Value)
            //              select new
            //              {
            //                  MaKhachHang = m.KHACHHANG.MaKhachHang,
            //                  TenKhachHang = m.KHACHHANG.TenKhachHang,
            //                  SoTienNo = m.TongTien - (from k in db.PHIEUTHUs
            //                                           where k.SoChungTu == m.SoChungTu
            //                                           select k).Sum(s => s.SoTien) + m.TienDaTra
            //              }).ToList();
            //    this.bsNo.DataSource = no;
            //    this.bsNo.ResetBindings(true);
            //}
        }
    }
}
